import {getWy,getPlatform} from 'uni_modules/wy-core/context'
const that={
	async kh_uploadFile(obj){
		var wy=getWy()
	 	let {url,_group,uploadTask}=obj
	 	let model={
	 		title:'上传中...',
	 		success:null,
	 		...obj
	 	}
	 	let {title,success}=model
	 	let {filePath,atc}=obj.data
		if(!atc){
			atc='common'
		} 
		var uploadAPI=wy.formatApiUrl(_group,{ac:'UploadImage',atc})
		console.log(uploadAPI)
	 	return new Promise((resolve, reject)=>{
			var task =uni.uploadFile({
				url:uploadAPI,
				filePath:filePath,
				name:'file',
				fail(e) {
					reject(e)
				},
				success(e) {
					
					let res=JSON.parse(e['data'])
					let {ret,data}=res
					if(ret==200)
					{ 
						if(data.data){
							data=data.data
						}
						if(success){
							success({fileID:data.url,...data})
						}
						resolve({
							fileID:data.url,
							...data
						})
					}
					else{
						reject(res)
					}
				},
				complete() {
					if(title){
						uni.hideLoading()
					}
				}
				
			}) 
			if(uploadTask&&wy.pubfn.isFunction(uploadTask)){
				uploadTask(task)
			}
	 	})
	 }
};
export default that